/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adminclient;

import ejb.MRSAdminManagerBeanRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.ejb.EJB;

/**
 *
 * @author cuilin
 */
public class Main {

    @EJB
    private static MRSAdminManagerBeanRemote mrs;
    static Scanner sc = new Scanner(System.in);
    //create new instance of admin

    public Main() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            while (true) {
                printMenu();
                String command = sc.nextLine();

                String commandName = "";
                boolean toContinue = true;
                while (toContinue) {
                    System.out.println();

                    if (command.equals("1")) {
                        commandName = "add student";
                        addStudent();
                    } else if (command.equals("2")) {
                        commandName = "delete student";
                        deleteStudent();
                    } else if (command.equals("3")) {
                        commandName = "add lecturer";
                        addLecturer();
                    } else if (command.equals("4")) {
                        commandName = "delete lecturer";
                        deleteLecturer();
                    } else if (command.equals("5")) {
                        commandName = "add module";
                        addModule();
                    } else if (command.equals("6")) {
                        commandName = "delete module";
                        deleteModule();
                    } else if (command.equals("7")) {
                        commandName = "add tutor";
                        addTutor();
                    } else if (command.equals("8")) {
                        commandName = "delete tutor";
                        deleteTutor();
                    } else if (command.equals("9")) {
                        commandName = "add tutorial";
                        addTutorial();
                    } else if (command.equals("10")) {
                        commandName = "delete tutorial";
                        deleteTutorial();
                    } else if (command.equals("11")) {
                        commandName = "view module detail";
                        viewModuleDetail();
                    } else if (command.equals("12")) {
                        commandName = "view tutorials";
                        viewTutorials();
                    } else if (command.equals("13")) {
                        commandName = "assign student to module";
                        assignStoM();
                    } else if (command.equals("14")) {
                        commandName = "assign student to tutorial";
                        assignStoT();
                    } else if (command.equals("15")) {
                        commandName = "process appeals";
                        processAppeals();
                    } else if (command.equalsIgnoreCase("q")) {
                        System.exit(0);
                    }

                    System.out.print("Continue to " + commandName + "(y), press any other key to return main menu...");
                    String subcmd = sc.nextLine();
                    if (!subcmd.equalsIgnoreCase("y")) {
                        toContinue = false;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Caught an unexpected exception ..." + ex.getMessage());
            System.exit(0);
        }
    }

    private static void printMenu() {
        System.out.println();
        System.out.println("*********************************************************");
        System.out.println("*  MRS: Administrator: Module Registration System       *");
        System.out.println("*********************************************************");
        System.out.println("---------------------------------------------------------");
        System.out.println("1. Add A Student       |  10. Delete A Tutorial ");
        System.out.println("2. Delete A Student    |  11. View Module Detail");
        System.out.println("3. Add A Lecturer      |  12. View Tutorials");
        System.out.println("4. Delete A Lecturer   |  13. Assign Student to Module");
        System.out.println("5. Add A Module        |  14. Assign Student to Tutorial");
        System.out.println("6. Delete A Module     |  15. Process Appeals");
        System.out.println("7. Add A Tutor         |  ");
        System.out.println("8. Delete A Tutor      |  ");
        System.out.println("9. Add A Tutorial      |  ");
        System.out.println("---------------------------------------------------------");
        System.out.print("Enter Your Command(Press 'Q' Or 'q' to Exit): ");
    }

    //add student
    private static void addStudent() {
        //add student
        System.out.println();
        System.out.print("Matric Number: ");
        String matricNumber = sc.nextLine();
        //TODO: check if account alr exist
        if (mrs.isExistStudent(matricNumber)) {
            System.out.println("Student account already existing.");
        } else {
            System.out.print("Name: ");
            String name = sc.nextLine();
            System.out.print("Password: ");
            String password = sc.nextLine();
            System.out.print("Email: ");
            String email = sc.nextLine();
            mrs.addStudent(matricNumber, name, password, email);
        }
    }

    //delete student
    private static void deleteStudent() {
        System.out.println();
        System.out.print("Matric Number: ");
        String matricNumber = sc.nextLine();
        //Check existence of student
        if (!mrs.isExistStudent(matricNumber)) {
            System.out.println("ERROR: Student account does not exist.");
            System.out.println();
        } else {
            //find studentID using matric number
            Long id = mrs.findStudentId(matricNumber);

            //cannot delete if associate with module
            if (mrs.isAssociateSM(id)) {
                System.out.println("ERROR: Student associated with module.");
                //TODO: cannot delete if associate with appeal
                //System.out.println("ERROR: Student associated with appeal.");
            } else {
                mrs.deleteStudent(id);
                //then delete student accounth
                System.out.println("Student account deleted.");
                System.out.println();
            }
        }
    }

    //add lecturer
    private static void addLecturer() {
        System.out.println();
        System.out.print("Staff Number: ");
        String staffNum = sc.nextLine();
        //check if lecturer exists
        if (mrs.isExistLecturer(staffNum)) {
            System.out.println("ERROR: The lecturer already exist.");
            System.out.println();
        } else {
            System.out.print("Title: ");
            String title = sc.nextLine();
            System.out.print("Name: ");
            String name = sc.nextLine();
            System.out.print("Email: ");
            String email = sc.nextLine();
            System.out.print("Office: ");
            String office = sc.nextLine();
            mrs.addLecturer(staffNum, title, name, email, office);
            Long lId = mrs.persistLecturer();
            System.out.println("Lecturer ID " + lId + " Created.");
            System.out.println();
        }

    }

    //delete lecturer
    private static void deleteLecturer() {
        System.out.println();
        System.out.print("Staff Number: ");
        String staffNum = sc.nextLine();
        //check if lecturer exists
        if (!mrs.isExistLecturer(staffNum)) {
            System.out.println("ERROR: The lecturer does not exist.");
            System.out.println();
        } else {
            //TODO:mrs.findLecturerId
            Long id = mrs.findLecturerId(staffNum);
            //check if associate with module
            if (mrs.isAsscociateLecturer(id)) {
                System.out.println("ERROR: The lecturer is associated with module.");
                System.out.println();
            } else {
                mrs.deleteLecturer(id);
                System.out.println("Lecturer deleted.");
            }
        }
    }

    //add module
    private static void addModule() {
        System.out.println();
        System.out.print("Module Code: ");
        String mcode = sc.nextLine();
        //check existence of module
        if (mrs.isExistModule(mcode)) {
            System.out.println("The module already exists.");
            System.out.println();
        } else {
            //add module
            System.out.print("Module Title: ");
            String mtitle = sc.nextLine();
            System.out.print("Time: ");
            String mtime = sc.nextLine();
            System.out.print("Venue: ");
            String mvenue = sc.nextLine();
            mrs.addModule(mcode, mtitle, mtime, mvenue);

            //Add lecturer
            System.out.print("Staff Number: ");
            String staffNum = sc.nextLine();
            //Check if the lecturer is already existing.
            List list = mrs.findLecturers(staffNum);
            if (null != list && !list.isEmpty()) {
                //associate to existing lecturer
                System.out.println("Existing lecturer, Asscociate with this new module");
                Long lId = (Long) list.get(0);
                mrs.associateModuleLecturer(lId);

            } else {
                //Create new lecturer
                System.out.print("Title: ");
                String title = sc.nextLine();
                System.out.print("Name: ");
                String name = sc.nextLine();
                System.out.print("Email: ");
                String email = sc.nextLine();
                System.out.print("Office: ");
                String office = sc.nextLine();
                mrs.addLecturer(staffNum, title, name, email, office);
            }
            //Add synopsis
            System.out.print("Enter Synopsis Content: ");
            String content = sc.nextLine();
            mrs.addSynopsis(content);
        }
        //Persist module
        Long mId = mrs.persistModule();
        System.out.println("Module ID " + mId + " was Created.");
        System.out.println();
        //check command
    }

    //delete module
    private static void deleteModule() {
        System.out.println();
        System.out.print("Module Code: ");
        String moduleCode = sc.nextLine();
        //check if module exists.
        if (mrs.isExistModule(moduleCode)) {
            //find module ID
            Long id = mrs.findModuleId(moduleCode);
            //TODO: cannot be deleted if registerd by a student
            //TODO: cannot be deleted if its tutorial registered by a student 
            mrs.deleteModule(id);
            System.out.println("Module deleted.");
        } else {
            System.out.println("ERROR: The module does not exist.");
        }
    }

    //add tutor
    private static void addTutor() {
        System.out.println();
        System.out.print("Staff Number: ");
        String staffNumber = sc.nextLine();
        //check if tutor exist.
        if (mrs.isExistTutor(staffNumber)) {
            System.out.println("ERROR: Tutor already exist.");
        } else {
            System.out.print("Name: ");
            String name = sc.nextLine();
            System.out.print("Email: ");
            String email = sc.nextLine();
            //add tutor
            mrs.addTutor(staffNumber, name, email);
        }
    }

    //delete tutor
    private static void deleteTutor() {
        System.out.println();
        System.out.print("Staff Number: ");
        String staffNumber = sc.nextLine();
        //check if tutor exist.
        if (!mrs.isExistTutor(staffNumber)) {
            System.out.println("ERROR: Tutor does not exist.");
        } else {
            //TODO: check if tutor associate with a tutorial
            //delete tutor
            mrs.deleteTutor(staffNumber);
        }
    }

    //add tutorial
    private static void addTutorial() {
        System.out.println();
        System.out.print("Module Code: ");
        String mcode = sc.nextLine();
        System.out.print("Tutorial Group Number: ");
        String groupNumber = sc.nextLine();
        //check existence of tutorial
        if (mrs.isExistTutorial(mcode, groupNumber)) {
            System.out.println("ERROR: The tutorial group already exist.");
            System.out.println();
        } else {
            System.out.print("Time: ");
            String t = sc.nextLine();
            System.out.print("Venue: ");
            String venue = sc.nextLine();
            mrs.addTutorial(groupNumber, t, venue, mcode);

            Long tutorialId = mrs.findTutorialId(mcode, groupNumber);
            //Add tutor
            System.out.print("Staff Number: ");
            String staffNum = sc.nextLine();
            //Check if the tutor is already existing.
            if (mrs.isExistTutor(staffNum)) {
                System.out.println("Existing tutor, Asscociate with this Tutorial");
                //find tutorial Id & tutor Id
                Long tutorId = mrs.findTutorId(staffNum);
                //associate tutor & tutorial
                mrs.associateTutorTutorial(tutorId, tutorialId);
                System.out.println();
            } else {
                //Create new tutor
                System.out.print("Name: ");
                String name = sc.nextLine();
                System.out.print("Email: ");
                String email = sc.nextLine();
                mrs.addTutor(staffNum, name, email);
                //find tutorial Id & tutor Id
                //Long tutorialId = mrs.findTutorialId(mcode, groupNumber);
                Long tutorId = mrs.findTutorId(staffNum);
                // associate tutor & tutorial
                mrs.associateTutorTutorial(tutorId, tutorialId);
            }
            System.out.println("Tutorial group added.");

        }
    }

    //delete tutorial
    private static void deleteTutorial() {
        System.out.println();
        System.out.print("Module Code: ");
        String mcode = sc.nextLine();
        System.out.print("Tutorial Group Number: ");
        String groupNumber = sc.nextLine();
        //check existence of tutorial
        if (!mrs.isExistTutorial(mcode, groupNumber)) {
            System.out.println("ERROR: The tutorial group does not exist.");
            System.out.println();
        } else {
            //TODO: check if registered by student
            //TODO: disassociate from tutor
            //TODO: delete tutorial
            mrs.deleteTutorial(mcode, groupNumber);
        }
    }

    //view module detail
    private static void viewModuleDetail() {
        System.out.println();
        System.out.print("Module Code: ");
        String moduleCode = sc.nextLine();
        //check existence of module
        if (mrs.isExistModule(moduleCode)) {
            //printout module details
            printModuleList(moduleCode);
        } else {
            System.out.println("Module " + moduleCode + " does not exist.");
        }
    }

    //view tutorials
    private static void viewTutorials() {
        System.out.print("Module Code: ");
        String moduleCode = sc.nextLine();
        //print tutorial details
        viewTutorialDetails(moduleCode);
    }

    //assign student to module
    private static void assignStoM() {
        System.out.println();
        System.out.print("Matric Number: ");
        String matricNumber = sc.nextLine();
        System.out.print("Module Code: ");
        String moduleCode = sc.nextLine();
        //check student/module existence
        if (!mrs.isExistModule(moduleCode) || !mrs.isExistStudent(matricNumber)) {
            System.out.println("ERROR: Invalid Matric Number or Module Code ");
        } else {
            Long studentId = mrs.findStudentId(matricNumber);
            Long moduleId = mrs.findModuleId(moduleCode);
            mrs.assignStudentModule(studentId, moduleId);
        }
    }

    //assign student to tutorial
    private static void assignStoT() {
        System.out.println();
        System.out.print("Matric Number: ");
        String matricNumber = sc.nextLine();
        //check if student exists
        if (mrs.isExistStudent(matricNumber)) {
            System.out.print("Module Code: ");
            String moduleCode = sc.nextLine();
            //check if module exists
            if (mrs.isExistModule(moduleCode)) {
                System.out.print("Tutorial Group Number: ");
                String groupNumber = sc.nextLine();
                Long studentId = mrs.findStudentId(matricNumber);
                Long tutorialId = mrs.findTutorialId(moduleCode, groupNumber);
                //assign student to tutorial
                mrs.assignStudentTutorial(studentId, tutorialId);
            } else {
                System.out.println("ERROR: Module does not exist.");
            }
        } else {
            System.out.println("ERROR: Student does not exist.");
        }
    }

    //process appeals
    private static void processAppeals() {
        //print appeal list
        printAppealList();
    }

    private static void printModuleList(String moduleCode) {
        System.out.println();
        System.out.println("*********************************");
        System.out.println("   Detailed Module Information   ");
        System.out.println("*********************************");
        System.out.println();
        ArrayList b = (ArrayList) mrs.viewModuleDetails(moduleCode);
        if (null == b || b.isEmpty()) {
            System.out.println("No Information Available For " + moduleCode);
        } else {
            System.out.println("Module Code: " + moduleCode);
            System.out.println("Module Title: " + b.get(0));
            System.out.println("Time: " + b.get(1));
            System.out.println("Venue: " + b.get(2));
            System.out.println("Lecturer: " + b.get(3));
            System.out.println("Synopsis: " + b.get(4));
            // get list of student(name,matric,email)

            List<ArrayList> students = (ArrayList) b.get(5);
            if (null != students && !students.isEmpty()) {
                System.out.println("List of registered students: ");
                for (ArrayList student : students) {
                    System.out.println(student.get(0) + " " + student.get(1) + " " + student.get(2));
                }
            }
        }
        System.out.println();
        System.out.println("*********************************");
    }

    private static void printAppealList() {
        System.out.println();
        System.out.println("*********************************");
        System.out.println("           Appeal List           ");
        System.out.println("*********************************");

        for (List appeal : mrs.viewAppeals()) {
            System.out.println();

            if (null == appeal || appeal.isEmpty()) {
                System.out.println("No appeal needs to be processed");
            } else {
                System.out.println("Appeal ID: " + appeal.get(0));
                System.out.println("Time: " + appeal.get(1));
                System.out.println("Content: " + appeal.get(2));
                System.out.println("Status: " + appeal.get(3));
                System.out.println("Comment: " + appeal.get(4));
                System.out.println();
            }
            System.out.println();
            System.out.println("*********************************");
        }
    }

    private static void viewTutorialDetails(String moduleCode) {
        System.out.println();
        System.out.println("*********************************");
        System.out.println("         Tutorial Details        ");
        System.out.println("*********************************");

        for (List tutorial : mrs.viewTutorialDetails(moduleCode)) {
            System.out.println();

            if (null == tutorial || tutorial.isEmpty()) {
                System.out.println("No tutorial for the module.");
            } else {
                System.out.println("Tutorial Group Number: " + tutorial.get(0));
                System.out.println("Time: " + tutorial.get(1));
                System.out.println("Venue: " + tutorial.get(2));
                //get tutor and registered students
                System.out.println("Tutor: " + tutorial.get(3));
                System.out.println("List of registered students: ");
                List<List> students = (List) tutorial.get(4);
                for (List student : students) {
                    System.out.println(student.get(0) + " " + student.get(1) + " " + student.get(2));
                }
            }
            System.out.println();
            System.out.println("*********************************");
        }
    }
}
